package org.hot100.h102;

import java.util.*;

/**
 * @Author: wjy
 * @Date: 2021/12/29 10:54
 */
public class Solution {

    /**
     * 给你一个二叉树，请你返回其按 层序遍历 得到的节点值。 （即逐层地，从左到右访问所有节点）。
     *
     * 示例：
     * 二叉树：[3,9,20,null,null,15,7],
     *
     * @param args
     */
    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode(3);
        TreeNode treeNode2 = new TreeNode(4);
        TreeNode treeNode3 = new TreeNode(2, treeNode1, treeNode2);
        TreeNode treeNode4 = new TreeNode(4);
        TreeNode treeNode5 = new TreeNode(3);
        TreeNode treeNode6 = new TreeNode(2, treeNode4, treeNode5);
        TreeNode treeNode7 = new TreeNode(1, treeNode3, treeNode6);

        Solution solution = new Solution();
        List<List<Integer>> lists = solution.levelOrder4(treeNode7);

        System.out.println(lists);
    }

    /**
     * bfs解法：利用bfs层级遍历的特点
     * 执行用时：2 ms, 在所有 Java 提交中击败了89.92%的用户
     * 内存消耗：38.5 MB, 在所有 Java 提交中击败了76.30%的用户
     *
     * @param root
     * @return
     */
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();

        Queue<TreeNode> queue = new ArrayDeque<>();
        if (root != null) {
            queue.add(root);
        }
        while (!queue.isEmpty()) {
            List<Integer> level = new ArrayList<>();

            int m = queue.size();

            // 队列的长度就是该层级元素的个数
            for (int i = 0; i < m; i++) {

                // 获取当前队列第一个元素
                TreeNode node = queue.poll();
                level.add(node.val);
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
            }

            res.add(level);
        }

        return res;
    }

    public List<List<Integer>> levelOrder2(TreeNode root) {

        List<List<Integer>> lists = new ArrayList<>();

        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);

        while (!queue.isEmpty()) {
            List<Integer> list = new ArrayList<>();
            int m = queue.size();
            for (int i = 0; i < m; i++) {
                TreeNode temp = queue.poll();
                list.add(temp.val);
                if (temp.left != null) {
                    queue.add(temp.left);
                }
                if (temp.right != null) {
                    queue.add(temp.right);
                }
            }
            lists.add(list);
        }
        return lists;
    }

    public List<List<Integer>> levelOrder3(TreeNode root) {

        List<List<Integer>> lists = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);

        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                list.add(node.val);
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.left != null) {
                    queue.add(node.right);
                }
            }
            lists.add(list);
        }
        return lists;
    }

    public List<List<Integer>> levelOrder4(TreeNode root) {
        List<List<Integer>> lists = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                list.add(node.val);
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
            }
            lists.add(list);
        }
        return lists;
    }

    /**
     * dfs解法
     * 执行用时：1 ms, 在所有 Java 提交中击败了89.92%的用户
     * 内存消耗：38.9 MB, 在所有 Java 提交中击败了5.01%的用户
     *
     */
/*    List<List<Integer>> res = new ArrayList<>();
    Map<Integer, List<Integer>> temp = new HashMap<>();
    public List<List<Integer>> levelOrder(TreeNode root) {
        if (root == null) {
            return new ArrayList<>();
        }

        this.dfs(root, 0);

        temp.forEach((k, v) -> {
            res.add(v);
        });

        return res;
    }

    public void dfs(TreeNode node, int i) {
        if (node == null) {
            return;
        }
        if (!temp.containsKey(i)) {
            List<Integer> list = new ArrayList<>();
            list.add(node.val);
            temp.put(i, list);
        } else {
            temp.get(i).add(node.val);
        }

        dfs(node.left, i + 1);
        dfs(node.right, i + 1);

    }*/

}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode() {
    }

    TreeNode(int val) {
        this.val = val;
    }

    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}